<?php

    //生成针对路由的URL
    function newU($url = '', $vars = array()) {
        $url = __APP__ . '/' . $url;
        if (!empty($vars)) {
            $vars = http_build_query($vars);
            $url .= '?'.$vars;
        }
        return $url;
    }

    //去除html标签及空格换行
    function delete_html($str) {
        $str = strip_tags($str);
        $str = str_replace(array("\t", "\r\n", "\r", "\n", " "), "", $str);
        return $str;
    }
    
    //utf8编码字符串截取函数
    function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') {
        if ($code == 'UTF-8') {
            $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
            preg_match_all($pa, $string, $t_string);

            if (count($t_string[0]) - $start > $sublen)
                return join('', array_slice($t_string[0], $start, $sublen)) . "...";
            return join('', array_slice($t_string[0], $start, $sublen));
        } else {
            $start = $start * 2;
            $sublen = $sublen * 2;
            $strlen = strlen($string);
            $tmpstr = '';
            for ($i = 0; $i < $strlen; $i++) {
                if ($i >= $start && $i < ($start + $sublen)) {
                    if (ord(substr($string, $i, 1)) > 129)
                        $tmpstr .= substr($string, $i, 2);
                    else
                        $tmpstr .= substr($string, $i, 1);
                }
                if (ord(substr($string, $i, 1)) > 129)
                    $i++;
            }
            if (strlen($tmpstr) < $strlen)
                $tmpstr .= "...";
            return $tmpstr;
        }
    }

    //字符串入库前处理
    function str_inmysql($str, $nohtml = '', $can_special = '') {
    	if ($nohtml == '1') {
    		$str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
    	} else {
    		$old_array = array('alert', '<script', 'onclick', 'javascript', 'onload', 'onerror', 'iframe', '<meta', '<input', '<textarea', 'onmouse');
    		$new_array = array('&#097;lert', '&#60;script', '&#111;nclick', '&#106;avascript', '&#111;nload', '&#111;nerror', '&#105;frame', '&#60;meta', '&#60;input', '&#60;textarea', '&#111;nmouse');
    		if ($can_special != '1') {
    			$str = str_ireplace($old_array, $new_array, $str);
    		}
    		$str = mysql_real_escape_string($str);
    	}
    	return $str;
    }

    //注册时验证码函数
    function send_reg_msg($mobile,$data){return 1;
        $post_data = array();
        $post_data['account'] = "yugu-01";
        $post_data['pswd'] = "Tch526228";
        $post_data['mobile'] = $mobile;
        $post_data['msg']=$data;
        $url='http://222.73.117.158/msg/HttpBatchSendSM?';
        $o="";
        foreach ($post_data as $k=>$v){
           $o.= "$k=".urlencode($v)."&";
        }
        $post_data=substr($o,0,-1);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
        $result = curl_exec ( $ch );
        curl_close ( $ch );

        $result = preg_split("/[,\r\n]/",$result);
        if($result[1]==0){
            return true;
        }else{
            return false;
        }
    }
    
    //随机函数
    function code_random($length = 6 , $numeric = 0) {
        PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
        if($numeric) {
            $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
            $hash = '';
            $chars = 'ABCDEFGHJKMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $length; $i++) {
                $hash .= $chars[mt_rand(0, $max)];
            }
        }
        return $hash;
    }
    
    function xml_to_array($xml){
        $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
        if(preg_match_all($reg, $xml, $matches)){
            $count = count($matches[0]);
            for($i = 0; $i < $count; $i++){
            $subxml= $matches[2][$i];
            $key = $matches[1][$i];
                if(preg_match( $reg, $subxml )){
                    $arr[$key] = xml_to_array( $subxml );
                }else{
                    $arr[$key] = $subxml;
                }
            }
        }
        return $arr;
    }
    
    //curl
    function curl_request($url,$data = null){
    	$curl = curl_init();
    	curl_setopt($curl, CURLOPT_URL, $url);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    	if (!empty($data)){
    		curl_setopt($curl, CURLOPT_POST, true);
    		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    	}
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    	$output = curl_exec($curl);
    	curl_close($curl);
    	return $output;
    }
    
    //格式化日期 2011-03-02 18:22:34 转换为 2011年3月2日
    function fmt_date($date) {
        $date = substr($date, 0, 10);
        $date_array = explode('-', $date);
        return $date_array[0] . '年' . $date_array[1] . '月' . $date_array[2] . '日';
    }

    //插入数组参数到一个url中
    function insertPramToUrl($uri = '', $vars = array()) {
        if (empty($uri)) return '';

        $info = parse_url($uri);
        if (isset($info['query'])) { // 解析地址里面参数 合并到vars
            parse_str($info['query'],$params);
            $vars = array_merge($params,$vars);
        }
        $vars = empty($vars) ? '' : http_build_query($vars);

        $url = '';
        if (!empty($info['scheme'])) $url = $info['scheme'].'://';
        $url .= $info['host'].$info['path'].'?'.$vars;
        if(!empty($info['fragment'])) $url .= '#'.$info['fragment'];
        return $url;
    }

    /**
     * 对openid和时间戳进行加密
     */
    function encodeOpenid ($openid = '', $timeStamp = 0) {
        if (empty($openid)) return '';

        $timeStamp = strval($timeStamp);
        $fromuser  = '';
        $len       = strlen($openid);

        for ($i = 0; $i < $len; $i++) {
            if (isset($timeStamp[$i])) {
                $fromuser .= $timeStamp[$i];
            }
            $fromuser .= $openid[$i];
        }

        return $fromuser;
    }

    /**
     * 对fromuser进行解密，解密出openid和时间戳
     */
    function decodeOpenid ($fromuser = '') {
        $len = strlen($fromuser);
        if (empty($fromuser) || $len <= 20) return array('status' => '0', 'openid' => '', 'timeStamp' => 0);

        $openid    = '';
        $timeStamp = '';

        for ($i = 0; $i < $len; $i++) {
            if ($i < 20) {
                if ($i % 2 == 0) {
                    $timeStamp .= $fromuser[$i];
                } else {
                    $openid .= $fromuser[$i];
                }
            } else {
                $openid .= $fromuser[$i];
            }
        }

        if ($timeStamp < (time() - 3600 * 24 * 300)) {
            return array('status' => '0', 'openid' => '', 'timeStamp' => 0);
        } else {
            return array('status' => '1', 'openid' => $openid, 'timeStamp' => intval($timeStamp));
        }
    }
